﻿  
  
 
using System;
using System.Collections.Generic;
using HumanWorkflow.Interface;

namespace OrderWorkflow.OrderWorkflowWithGeneratedCode
{
	public partial class OrderWorkflowWithGeneratedCode : IHumanWorkflow
	{
		public string CurrentStep { get; private set; }

	    public OrderWorkflowWithGeneratedCode()
	    {
			CurrentStep = "Start";
	    }

        public List<string> GetValidTransitions()
        {
			var result = new List<string>();

			if (CurrentStep ==  "Start")
			{
				result.Add("Start");
			}

			if (CurrentStep ==  "ConcordeCustomerData")
			{
				result.Add("ConcordanceReady");
			}

			if (CurrentStep ==  "CheckPossibleDiscounts")
			{
				result.Add("DiscountCheckReady");
			}

			if (CurrentStep ==  "AreAllItemsOnStock")
			{
				result.Add("ItemsAreMissing");
			}

			if (CurrentStep ==  "AreAllItemsOnStock")
			{
				result.Add("ItemsAreAvailable");
			}

			if (CurrentStep ==  "SendNotificationEmailToCustomer")
			{
				result.Add("EmailSent");
			}

			if (CurrentStep ==  "ShipItems")
			{
				result.Add("ItemsShipped");
			}

			if (CurrentStep ==  "OrderMissingItems")
			{
				result.Add("MissingItemsOrderDone");
			}

			if (CurrentStep ==  "WaitForItemsToArrive")
			{
				result.Add("MissingItemsArrived");
			}

			if (CurrentStep ==  "ConcordeCustomerData")
			{
				result.Add("ConcordeCustomerDataStopWorkflow");
			}

			if (CurrentStep ==  "CheckPossibleDiscounts")
			{
				result.Add("CheckPossibleDiscountsStopWorkflow");
			}

			if (CurrentStep ==  "SendNotificationEmailToCustomer")
			{
				result.Add("SendNotificationEmailToCustomerStopWorkflow");
			}

			if (CurrentStep ==  "ShipItems")
			{
				result.Add("ShipItemsStopWorkflow");
			}

			if (CurrentStep ==  "OrderMissingItems")
			{
				result.Add("OrderMissingItemsStopWorkflow");
			}

			if (CurrentStep ==  "WaitForItemsToArrive")
			{
				result.Add("WaitForItemsToArriveStopWorkflow");
			}

			if (CurrentStep ==  "Start")
			{
				result.Add("StartStopWorkflow");
			}

			if (CurrentStep ==  "AreAllItemsOnStock")
			{
				result.Add("AreAllItemsOnStockStopWorkflow");
			}

			return result;
        }

        public void RaiseTransition(string transitionName)
        {
			if (!GetValidTransitions().Contains(transitionName))
            {
                throw new ArgumentException("The provided transition is not valid for the current step.");
            }

			bool isAutomatic = false;
			bool isDecision = false;
			string nextAutomaticTransitionName = "";
			
			if (transitionName ==  "Start")
			{
				CurrentStep = "ConcordeCustomerData";
				isAutomatic = false;
				isDecision = false;
			}
			else if (transitionName == "StartStopWorkflow" )
			{
				CurrentStep = "Finish";
				isAutomatic = false;
				isDecision = false;
			}

			if (transitionName ==  "ConcordanceReady")
			{
				CurrentStep = "CheckPossibleDiscounts";
				isAutomatic = false;
				isDecision = false;
			}
			else if (transitionName == "ConcordeCustomerDataStopWorkflow" )
			{
				CurrentStep = "Finish";
				isAutomatic = false;
				isDecision = false;
			}

			if (transitionName ==  "DiscountCheckReady")
			{
				CurrentStep = "AreAllItemsOnStock";
				isAutomatic = false;
				isDecision = true;
			}
			else if (transitionName == "CheckPossibleDiscountsStopWorkflow" )
			{
				CurrentStep = "Finish";
				isAutomatic = false;
				isDecision = false;
			}

			if (transitionName ==  "ItemsAreMissing")
			{
				CurrentStep = "SendNotificationEmailToCustomer";
				isAutomatic = true;
				isDecision = false;
			}
			else if (transitionName == "AreAllItemsOnStockStopWorkflow" )
			{
				CurrentStep = "Finish";
				isAutomatic = false;
				isDecision = false;
			}

			if (transitionName ==  "ItemsAreAvailable")
			{
				CurrentStep = "ShipItems";
				isAutomatic = false;
				isDecision = false;
			}
			else if (transitionName == "AreAllItemsOnStockStopWorkflow" )
			{
				CurrentStep = "Finish";
				isAutomatic = false;
				isDecision = false;
			}

			if (transitionName ==  "EmailSent")
			{
				CurrentStep = "OrderMissingItems";
				isAutomatic = false;
				isDecision = false;
			}
			else if (transitionName == "SendNotificationEmailToCustomerStopWorkflow" )
			{
				CurrentStep = "Finish";
				isAutomatic = false;
				isDecision = false;
			}

			if (transitionName ==  "ItemsShipped")
			{
				CurrentStep = "Finish";
				isAutomatic = false;
				isDecision = false;
			}
			else if (transitionName == "ShipItemsStopWorkflow" )
			{
				CurrentStep = "Finish";
				isAutomatic = false;
				isDecision = false;
			}

			if (transitionName ==  "MissingItemsOrderDone")
			{
				CurrentStep = "WaitForItemsToArrive";
				isAutomatic = false;
				isDecision = false;
			}
			else if (transitionName == "OrderMissingItemsStopWorkflow" )
			{
				CurrentStep = "Finish";
				isAutomatic = false;
				isDecision = false;
			}

			if (transitionName ==  "MissingItemsArrived")
			{
				CurrentStep = "ShipItems";
				isAutomatic = false;
				isDecision = false;
			}
			else if (transitionName == "WaitForItemsToArriveStopWorkflow" )
			{
				CurrentStep = "Finish";
				isAutomatic = false;
				isDecision = false;
			}

            OnBeforeStep(new EventArgs());

            if (isDecision || isAutomatic)
            {
                nextAutomaticTransitionName = ExecuteAutomaticStep(CurrentStep);    
            }

            OnAfterStep(new EventArgs());

            if (isDecision || isAutomatic)
            {
                RaiseTransition(nextAutomaticTransitionName);
            }
        }

		public string CurrentRole
        {
            get 
            {
				if (CurrentStep == "ConcordeCustomerData")
				{
					return "CustomerCare"; 
				}
				if (CurrentStep == "CheckPossibleDiscounts")
				{
					return "CustomerCare"; 
				}
				if (CurrentStep == "ShipItems")
				{
					return "Inventory"; 
				}
				if (CurrentStep == "OrderMissingItems")
				{
					return "OrderHandling"; 
				}
				if (CurrentStep == "WaitForItemsToArrive")
				{
					return "OrderHandling"; 
				}
				throw new Exception("Role is only applicable to manual steps.");
			}
        }

		public event EventHandler BeforeStep;

		void OnBeforeStep(EventArgs e)
		{
			if (BeforeStep != null)
				BeforeStep(this, e);
		}

		public event EventHandler AfterStep;

		void OnAfterStep(EventArgs e)
		{
			if (AfterStep != null)
				AfterStep(this, e);            
		}
	}
}

